package com.xayy.ds.service.impl;

import com.xayy.scheduled.CronTaskRegistrar;
import com.xayy.scheduled.SchedulingRunnable;
import com.xayy.scheduled.enums.AssessTaskStatus;
import com.xtck.ha.common.core.web.service.dsimpl.BaseServiceImpl;
import com.xayy.ds.model.DsAssessTaskInfo;
import com.xayy.ds.service.DsAssessTaskInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * @Description: AssessTaskInfoService实现类
 * @Auther:zhoutanxin
 * @Date:2025-06-30
 */
@Slf4j
@Service("dsAssessTaskInfoService")
@AllArgsConstructor
public class DsAssessTaskInfoServiceImpl extends BaseServiceImpl<DsAssessTaskInfo> implements DsAssessTaskInfoService {
    private final CronTaskRegistrar cronTaskRegistrar;
  /**
  *
  * 冗余代码，为自定义开发DS-Server接口服务
  *
  */
   private static String TABLE_NAME = "ASSESS_TASK_INFO";
   private String getUrl(){
      return getDsServerConfig().getUrl();
   }

    @Override
    public void toggleStartPause(Long jobId) {
        DsAssessTaskInfo existedSysJob=this.selectTById(jobId);
        if (existedSysJob.getTaskStatus().equals(AssessTaskStatus.PAUSE.getValue())) {
            SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getId(),existedSysJob.getBeanName(), existedSysJob.getMethodName(), String.valueOf(existedSysJob.getIndexSystemId()));
            cronTaskRegistrar.addCronTask(task, existedSysJob.getCronExpression());
            existedSysJob.setTaskStatus(AssessTaskStatus.NORMAL.getValue());
        } else if (existedSysJob.getTaskStatus().equals(AssessTaskStatus.NORMAL.getValue())){
            SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getId(),existedSysJob.getBeanName(), existedSysJob.getMethodName(), String.valueOf(existedSysJob.getIndexSystemId()));
            cronTaskRegistrar.removeCronTask(task);
            existedSysJob.setTaskStatus(AssessTaskStatus.PAUSE.getValue());
        }
        this.updateT(existedSysJob);
    }
}
